package server;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread{

    private Socket socket;
    private Map<String, HttpServlet> servletMap;
    private String appBase;

    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap, String appBase){
        this.socket = socket;
        this.servletMap = servletMap;
        this.appBase = appBase;
    }
    public void run(){

        try{
            InputStream inputStream = socket.getInputStream();

            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            if(servletMap.get(request.getUrl()) == null){
                response.ouputHtml(appBase, request.getUrl());
            }else{
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.doGet(request, response);
            }
            response.ouputHtml(appBase, request.getUrl());

            socket.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
